interface Options {
  headers?: Record<string, string>
  [key: string]: any
}

export const useHttpFetch = (url: string, opt: Options) => {
  const token = useCookie('token')
  const headers = {
    ...opt.headers,
    ...(token ? { Authorization: `Bearer ${token}` } : {}),
  }
  opt.headers = headers
  return useFetch(url, {
    ...opt,
    baseURL: '',
    onRequest({ request, options }) {
      // 设置请求头
      console.log('onRequest', request)
    },
    onRequestError({ request, options, error }) {
      // 处理请求错误
      console.log('onRequestError', request)
    },
    onResponse({ request, response, options }) {
      // 处理响应数据
      console.log('onResponse', request)
    },
    onResponseError({ request, response, options }) {
      // 处理响应错误
      console.log('onResponseError', request)
    },
  })
}

export const userInfoFetch = (opt: Options) => {
  return useHttpFetch('/api/user/info', opt)
}
